package doc2db.interfaces.xmledit;

import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import doc2db.Keey;
import doc2db.Launcher;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

/**
 *
 * @author yuriy
 */
public class XmlEditorMenu {
    private CancelAction cancelAction = new CancelAction();
    private HelpAction helpAction = new HelpAction();

    private Font s = UserProp.menuFont;
    private String toolFont = "";
            //"<html><table bgcolor=yellow border=1><tr><th><font size=\"4\" face=\"Verdana\">";
    private String str = FileBeginer.resurse_resurseFolderMenu+Keey.sp;
    private XmlEditorFrame fr;

    private JMenu menu = new JMenu("Меню");
    private JMenuItem save = new JMenuItem("Сохранить");
    private JMenuItem cancel = new JMenuItem("Отмена");
    
    private JMenu correct = new JMenu("Правка");
    private JMenuItem chekXML = new JMenuItem("Проверить XML");
    private JMenuItem testLex = new JMenuItem("Проверка выражений");


    private JMenu mhelp = new JMenu("Помощь");
    private JMenuItem help = new JMenuItem("Спец-символы");


    public XmlEditorMenu(XmlEditorFrame f){
        fr = f;
        this.setFont(this.s);
        this.setToolTipeText();
        this.setAction();
        this.setAccelerators();
        this.setIcons();
    }

    JMenu getMenu(){

        menu.add(save);
        menu.addSeparator();
        menu.add(cancel);

        return menu;
    }

    private void setAccelerators() {
        int ctrl = InputEvent.CTRL_MASK;
        int shft = InputEvent.SHIFT_MASK;
        
        save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ctrl));
        cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ctrl));
        help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
        chekXML.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, shft));
        testLex.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, shft));
    }
    
    JMenu getHelp(){
        mhelp.add(help);
        return mhelp;
    }

    /**
     * Create Correct menu with chek error and test menu itemes
     * @param chek action for chek error menu iteme
     * @param test action for test menu iteme
     * @return correct menu
     */
    JMenu getCorrect(ActionListener chek, ActionListener test){
        chekXML.addActionListener(chek);
        testLex.addActionListener(test);

        correct.add(chekXML);
        correct.add(testLex);
        
        return correct;
    }


    public void setFont(Font f){
        menu.setFont(f);
        save.setFont(f);
        cancel.setFont(f);
        mhelp.setFont(f);
        help.setFont(f);
        correct.setFont(f);
        chekXML.setFont(f);
        testLex.setFont(f);
    }

    private void setIcons(){
        String sn = str + "save.png";
        String cn = str + "cancel.png";
        String hn = str + "help.png";
        String xn = str + "chekXML.png";
        String tn = str + "testLex.png";
        String path = "menu/";
        
        save.setIcon(new  ImageIcon(sn));
        cancel.setIcon(new  ImageIcon(cn));
        help.setIcon(new  ImageIcon(hn));
        chekXML.setIcon(new  ImageIcon(xn));
        testLex.setIcon(new  ImageIcon(tn));

        if (!new File(sn).exists()) {
            save.setIcon(Launcher.getIcon(path + "save.png"));
        }
        if (!new File(cn).exists()) {
            cancel.setIcon(Launcher.getIcon(path + "cancel.png"));
        }
        if (!new File(hn).exists()) {
            help.setIcon(Launcher.getIcon(path + "help.png"));
        }
        if (!new File(cn).exists()) {
            chekXML.setIcon(Launcher.getIcon(path + "editor/chekXML.png"));
        }
        if (!new File(hn).exists()) {
            testLex.setIcon(Launcher.getIcon(path + "editor/testLex.png"));
        }
    }

    private void setToolTipeText(){
        save.setToolTipText(toolFont + "Создать шаблон");
        cancel.setToolTipText(toolFont + "Отменить создание шаблона");
        help.setToolTipText(toolFont + "Вызвать справку");
        chekXML.setToolTipText(toolFont + "Поверить правельность XML дерева");
        testLex.setToolTipText(toolFont + "Проверка регулярных выражений");

    }

    private void setAction(){
        save.addActionListener(null);
        cancel.addActionListener(cancelAction);
        help.addActionListener(helpAction);
    }

    public void addSaveAction(ActionListener saveAction){
        save.addActionListener(saveAction);
    }

    ActionListener getCancelAction() {
        return cancelAction;
    }

    ActionListener getHelpAction() {
        return helpAction;
    }



    class HelpAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            HelpFrame hf = new HelpFrame(fr);
            hf.setVisible(true);
        }
    }

    class CancelAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            fr.setVisible(false);
        }
    }
}
